package com.ruoyi.third.service.impl;

import java.util.List;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.manufacturer.IPlatform;
import com.ruoyi.manufacturer.PlatformTask;
import com.ruoyi.third.domain.ThirdConfigField;
import com.ruoyi.third.mapper.ThirdConfigFieldMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.third.mapper.ThirdPlatformMapper;
import com.ruoyi.third.domain.ThirdPlatform;
import com.ruoyi.third.service.IThirdPlatformService;

/**
 * 接入平台Service业务层处理
 * 
 * @author yepanpan
 * @date 2025-02-06
 */
@Service
public class ThirdPlatformServiceImpl implements IThirdPlatformService 
{
    private static final String CACHE_MAP = "third_platform";
    private static final Logger log = LoggerFactory.getLogger(ThirdPlatformServiceImpl.class);

    @Autowired
    private RedisCache redisCache;
    @Autowired
    private ThirdPlatformMapper thirdPlatformMapper;
    @Autowired
    private ThirdConfigFieldMapper thirdConfigFieldMapper;

    /**
     * 清除缓存
     * @return
     */
    @Override
    public int reCache(){
        redisCache.deleteObject(CACHE_MAP);
        return 1;
    }

    /**
     * 查询接入平台
     *
     * @param code 接入平台代码
     * @return 接入平台
     */
    @Override
    public ThirdPlatform selectThirdPlatformByCode(String code){
        ThirdPlatform thirdPlatform = redisCache.getCacheMapValue(CACHE_MAP, code);
        if(thirdPlatform != null){
            return thirdPlatform;
        }
        thirdPlatform = thirdPlatformMapper.selectThirdPlatformByCode(code);
        if(thirdPlatform == null){
            return null;
        }
        thirdPlatform.setConfig(thirdConfigFieldMapper.selectThirdPlatformConfig(code));
        redisCache.setCacheMapValue(CACHE_MAP, code, thirdPlatform);
        return thirdPlatform;
    }

    /**
     * 查询接入平台
     * 
     * @param id 接入平台主键
     * @return 接入平台
     */
    @Override
    public ThirdPlatform selectThirdPlatformById(Long id)
    {
        ThirdPlatform thirdPlatform = thirdPlatformMapper.selectThirdPlatformById(id);
        if(thirdPlatform == null){
            return null;
        }

        thirdPlatform.setConfig(thirdConfigFieldMapper.selectThirdPlatformConfig(thirdPlatform.getCode()));
        redisCache.setCacheMapValue(CACHE_MAP, thirdPlatform.getCode(), thirdPlatform);
        return thirdPlatform;
    }

    /**
     * 查询接入平台列表
     * 
     * @param thirdPlatform 接入平台
     * @return 接入平台
     */
    @Override
    public List<ThirdPlatform> selectThirdPlatformList(ThirdPlatform thirdPlatform)
    {
        return thirdPlatformMapper.selectThirdPlatformList(thirdPlatform);
    }

    /**
     * 新增接入平台
     * 
     * @param thirdPlatform 接入平台
     * @return 结果
     */
    @Override
    public int insertThirdPlatform(ThirdPlatform thirdPlatform)
    {
        thirdPlatform.setCreateTime(DateUtils.getNowDate());
        int ret = thirdPlatformMapper.insertThirdPlatform(thirdPlatform);

        //初始化配置
        ThirdConfigField thirdConfigField = new ThirdConfigField();
        thirdConfigField.setCreateTime(thirdPlatform.getCreateTime());
        thirdConfigField.setCreateBy(thirdPlatform.getCreateBy());
        thirdConfigField.setPfCode(thirdPlatform.getCode());
        ret += thirdConfigFieldMapper.initThirdConfigValue(thirdConfigField);

        return ret;
    }

    /**
     * 修改接入平台
     * 
     * @param thirdPlatform 接入平台
     * @return 结果
     */
    @Override
    public int updateThirdPlatform(ThirdPlatform thirdPlatform)
    {
        thirdPlatform.setUpdateTime(DateUtils.getNowDate());

        //初始化配置
        ThirdConfigField thirdConfigField = new ThirdConfigField();
        thirdConfigField.setCreateTime(thirdPlatform.getUpdateTime());
        thirdConfigField.setCreateBy(thirdPlatform.getUpdateBy());
        thirdConfigField.setPfCode(thirdPlatform.getCode());
        log.debug("----------ThirdConfigField：{}", thirdConfigField);
        thirdConfigFieldMapper.initThirdConfigValue(thirdConfigField);

        return thirdPlatformMapper.updateThirdPlatform(thirdPlatform) + reCache();
    }

    /**
     * 批量删除接入平台
     * 
     * @param ids 需要删除的接入平台主键
     * @return 结果
     */
    @Override
    public int deleteThirdPlatformByIds(Long[] ids)
    {
        return thirdPlatformMapper.deleteThirdPlatformByIds(ids) + reCache();
    }

    /**
     * 删除接入平台信息
     * 
     * @param id 接入平台主键
     * @return 结果
     */
    @Override
    public int deleteThirdPlatformById(Long id)
    {
        return thirdPlatformMapper.deleteThirdPlatformById(id) + reCache();
    }

    /**
     * 启动/停止接入平台
     *
     * @param cmd start/stop
     * @param id 接入平台主键
     * @return 结果
     */
    @Override
    public int triggerThirdPlatform(String cmd, Long id){
        ThirdPlatform thirdPlatform = thirdPlatformMapper.selectThirdPlatformById(id);
        if(thirdPlatform == null){
            return 0;
        }
        IPlatform handler = PlatformTask.getHandler(thirdPlatform.getCode());
        if("start".equals(cmd)){
            if(thirdPlatform.getRunning().equals("1")){
                return 1;
            }else{
                Boolean ret = handler.start(thirdPlatform);
                if(ret){
                    thirdPlatform.setRunning("1");
                    thirdPlatformMapper.updateThirdPlatform(thirdPlatform);
                    return 1;
                }
            }
        }else if("stop".equalsIgnoreCase(cmd)){
            if(thirdPlatform.getRunning().equals("0")){
                return 1;
            }else{
                Boolean ret = handler.stop(thirdPlatform);
                if(ret){
                    thirdPlatform.setRunning("0");
                    thirdPlatformMapper.updateThirdPlatform(thirdPlatform);
                    return 1;
                }
            }
        }else{
            throw new ServiceException("不支持的操作");
        }
        return 0;
    }

}
